package no.group.gowther.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import no.group.gowther.dao.OnePieceTagDao;
import no.group.gowther.service.dto.TagDTO;
import no.group.gowther.dao.entity.OnePieceTag;
import no.group.gowther.dao.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OnePieceTagServiceImpl extends ServiceImpl<OnePieceTagDao, OnePieceTag> implements OnePieceTagService {

    @Autowired
    private TagService tagService;

    @Override
    public List<TagDTO> queryTag(Long onePieceId) {
        final List<Tag> tags = baseMapper.queryList(onePieceId);
        return tags.stream().map(tag -> new TagDTO(tag.getId(), tag.getName(), tag.extractLastName())).collect(Collectors.toList());
    }

    @Override
    public long countReferNum(Long tagId) {
        List<Long> tagIds = tagService.queryDescendantTag(Collections.singletonList(tagId));
        return count(Wrappers.<OnePieceTag>lambdaQuery().in(OnePieceTag::getTagId, tagIds));
    }
}
